package com.lcq.gulimall.ware.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.lcq.common.exception.BizCodeEnum;
import com.lcq.common.to.OrderItemLockTo;
import com.lcq.gulimall.ware.vo.SkuStockVo;
import com.lcq.gulimall.ware.vo.WareSkuLockVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.lcq.gulimall.ware.entity.WareSkuEntity;
import com.lcq.gulimall.ware.service.WareSkuService;
import com.lcq.common.utils.PageUtils;
import com.lcq.common.utils.R;



/**
 * 商品库存
 *
 * @author lichuanqi
 * @email lichuanqi0086@163.com
 * @date 2021-02-21 18:00:14
 */
@RestController
@RequestMapping("ware/waresku")
public class WareSkuController {
    @Autowired
    private WareSkuService wareSkuService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("ware:waresku:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = wareSkuService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 是否有货
     */
    @RequestMapping("/hasStock/{skuId}")
    //@RequiresPermissions("ware:waresku:list")
    public R hasStock(@PathVariable("skuId") Long skuId){
        Boolean hasStock = wareSkuService.hasStock(skuId);
        return R.ok().put("hasStock", hasStock);
    }

    /**
     * 是否有货 批量获取
     */
    @PostMapping("/hasStocks")
    //@RequiresPermissions("ware:waresku:list")
    public List<SkuStockVo> hasStocks(@RequestBody List<Long> skuIds){
        List<SkuStockVo> list = wareSkuService.hasStocks(skuIds);
        return list;
    }

    /**
     * 锁库存
     */
    @PostMapping("/lockWare")
    public R lockWare(@RequestBody WareSkuLockVo wareSkuLockVo){
        try {
            wareSkuService.lockWare(wareSkuLockVo);
            return R.ok();
        }catch (Exception e){
            return R.error(BizCodeEnum.WARE_OUT_OF_STOCK.getCode(),BizCodeEnum.WARE_OUT_OF_STOCK.getMsg());
        }
    }



    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("ware:waresku:info")
    public R info(@PathVariable("id") Long id){
		WareSkuEntity wareSku = wareSkuService.getById(id);

        return R.ok().put("wareSku", wareSku);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("ware:waresku:save")
    public R save(@RequestBody WareSkuEntity wareSku){
		wareSkuService.save(wareSku);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("ware:waresku:update")
    public R update(@RequestBody WareSkuEntity wareSku){
		wareSkuService.updateById(wareSku);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("ware:waresku:delete")
    public R delete(@RequestBody Long[] ids){
		wareSkuService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
